﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMathLibrary.NonlinearEquationSolvers;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides to find interception points of two polynomials.
    /// </summary>
    [Serializable]
    public class PolynomialInterceptPointFinder
    {
        /// <summary>
        /// The first polynomial for the interception calculation.
        /// </summary>
        private SimplePolynomial firstPolynomial;

        /// <summary>
        /// The second polynomial for the interception calculation.
        /// </summary>
        private SimplePolynomial secondPolynomial;

        /// <summary>
        /// Initializes a new instance of the <see cref="PolynomialInterceptPointFinder"/> class.
        /// </summary>
        /// <param name="firstPolynomial">The first polynomial for the interception calculation.</param>
        /// <param name="secondPolynomial">The second polynomial for the interception calculation.</param>
        public PolynomialInterceptPointFinder(SimplePolynomial firstPolynomial, SimplePolynomial secondPolynomial)
        {
            this.firstPolynomial = firstPolynomial;
            this.secondPolynomial = secondPolynomial;
        }

        /// <summary>
        /// Gets or sets the first polynomial for the interception calculation.
        /// </summary>
        /// <value>The first polynomial for the interception calculation.</value>
        public SimplePolynomial FirstPolynomial
        {
            get { return firstPolynomial; }
            set { firstPolynomial = value; }
        }

        /// <summary>
        /// Gets or sets the second polynomial for the interception calculation.
        /// </summary>
        /// <value>The second polynomial for the interception calculation.</value>
        public SimplePolynomial SecondPolynomial
        {
            get { return secondPolynomial; }
            set { secondPolynomial = value; }
        }

        /// <summary>
        /// Finds the interception points of the specified polynomials. The roots of the difference
        /// polynomial would be calculated by the Bairstow algorithm.
        /// </summary>
        /// <returns>The positions where both polynomials intercept.</returns>
        public ComplexNumber[] FindIntercectionPoints()
        {
            SimplePolynomial workingPolynomial = this.secondPolynomial - this.firstPolynomial;
            BairstowComplexRootFinder rootFinder = new BairstowComplexRootFinder(workingPolynomial);

            return rootFinder.FindRoots();
        }
    }
}